import { Common } from "UtilsCommon";
import Expand from './actions/expand';
import aopc from 'aopc';

export default class Index {
  constructor(props) {
    this.receives = {};
    this.minActionType = 0;
    this.maxActionType = 0;
    for (const key in props) this[key] = props[key];
  }

  init() {
    if (!this.actionTypes) this.actionTypes = this.getActionTypes(this.name);
    this.actionTypeKeys = this.getActionTypeKeys();
  }

  initExpand() {
    if (Expand[this.name]) {
      const expand = Expand[this.name];
      for (const key in expand) this[key] = expand[key];
    }

    const expand2 = aopc.getExpandDataActions(this.minActionType);
    if (expand2) for (const key in expand2) this[key] = expand2[key];
  }

  getActionTypeKeys() {
    const actionTypeKeys = {};
    for (const key in this.actionTypes) actionTypeKeys[this.actionTypes[key]] = key;
    return actionTypeKeys;
  }

  invoke(id, actionType, data) {
    const key = this.actionTypeKeys[actionType];
    if (!this[key] && data.dataAction) {
      const { methodName } = data.dataAction;
      if (this[methodName]) this[key] = this[methodName];
    }
    if (this[key]) this[key](id, actionType, data);
    else this.dispatch(id, actionType, data);
  }

  dispatch(id, actionType, data) {
    data = this.setResponseData(id, actionType, data);
    if (data === false) return;
    this.dispatchToReceive(id, actionType, data);
  }

  dispatchToReceive(id, actionType, data) {
    if (id && this.receives[id]) this.receives[id](actionType, data);
    else for (const key in this.receives) this.receives[key](actionType, data);
  }

  setSearchQueryResponse(data, dataName) {
    data = this.setApiResponse(data);
    if (data.length >= 0) data = { Data: data };
    if (data.Data) {
      if (data.Data.isSuccess === false) return data.Data;
      const dataList = data.Data || [];
      let pageInfo = null; let groupByInfo = null;

      if (data.PageInfo) {
        const { PageIndex, PageSize, PageCount, PageRecord } = data.PageInfo;
        pageInfo = { pageIndex: PageIndex, pageSize: PageSize, pageCount: PageCount, pageRecord: PageRecord };
      }
      groupByInfo = data.GroupByInfo;

      return { dataList, pageInfo, groupByInfo };
    }
    else if (data[dataName] !== undefined) {
      const dataList = data[dataName] || [];
      const pageRecord = dataList.length;
      return { pageRecord, dataList };
    }
    return data;
  }

  setApiResponse(data) {
    if (data.action && data.data) {
      data = data.data;
      if (data.code !== undefined) {
        if (Common.isEquals(data.code, 0)) return { isSuccess: true, data };
        return { isSuccess: false, message: data.message };
      }
    }
    return data;
  }

  setResponseData(id, actionType, data) {
    if (data && data.action && data.action.isCheckedId && !this.receives[id]) return false;
    const key = `set${this.actionTypeKeys[actionType]}`;
    if (this[key]) return this[key](id, actionType, data);
    return this.setApiResponse(data);
  }

  receive(id, fn) {
    this.receives[id] = fn;
  }

  removeReceive(id) {
    if (this.receives[id]) delete this.receives[id];
  }

  getAction(id, actionType, isCheckedId) {
    isCheckedId = isCheckedId === undefined ? true : isCheckedId;
    return { id, actionType, time: new Date().getTime(), isCheckedId };
  }

  judgeNullable(data, entity) {
    let msg = "";
    for (const key in entity) {
      if (Common.isNullOrEmpty(data[key])) {
        msg = entity[key];
        break;
      }
    }
    return msg;
  }
}
